/**
 * struct ListNode {
 *	int val;
 *	struct ListNode *next;
 * };
 */

class Solution {
public:
    /**
     * 
     * @param l1 ListNode类 
     * @param l2 ListNode类 
     * @return ListNode类
     */
    ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
        // write code here
        int carry = 0;
        if (!l1 && !l2) return nullptr;
        ListNode *dummy = new ListNode(0);
        ListNode *pre = dummy;
        while (l1 || l2) {
            ListNode *node = new ListNode(0);
            int val1 = l1 ? l1->val : 0;
            int val2 = l2 ? l2->val : 0;
            int add = val1 + val2 + carry;
            carry = add / 10;
            add %= 10;
            node->val = add;
            pre->next = node;
            pre = node;
            l1 = l1 ? l1->next : nullptr;
            l2 = l2 ? l2->next : nullptr;
        }
        if (carry)
            pre->next = new ListNode(carry);
        return dummy->next;
    }
};
